import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/ladder_tab_bar.dart';
import 'package:ball_lion/resources/colours.dart';

class Block extends StatelessWidget {
  final CardStyle cardStyle;
  final Function()? onTap;
  final Widget child;

  const Block(
      {super.key,
      required this.child,
      this.cardStyle = const CardStyle(
          radius: 8,
          color: Colours.white,
          shadow: BoxShadow(
            color: Colours.transparent,
            offset: Offset(0, 2),
            blurRadius: 12,
          )),
      this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          clipBehavior: Clip.antiAlias,
          margin: cardStyle.margin,
          padding: cardStyle.padding,
          height: cardStyle.height,
          decoration: BoxDecoration(
            color: cardStyle.color ?? Colours.white,
            gradient: cardStyle.gradient,
            borderRadius: BorderRadius.circular(cardStyle.radius ?? 8),
            image: cardStyle.backImg.noValue
                ? null
                : DecorationImage(
                    fit: BoxFit.cover,
                    image:
                        AssetImage(Utils.getImgPath(cardStyle.backImg ?? ""))),
            boxShadow: [
              cardStyle.shadow ??
                  const BoxShadow(
                    color: Colours.transparent,
                    offset: Offset(0, 2),
                    blurRadius: 12,
                  ),
            ],
          ),
          child: child),
    );
  }
}

class TabBlock extends StatefulWidget {
  const TabBlock(
      {super.key,
      this.cardStyle,
      this.onTap,
      required this.tabs,
      required this.pages,
      this.initialIndex,
      this.selectLabelStyle,
      this.labelStyle});

  final CardStyle? cardStyle;
  final ValueChanged<int>? onTap;
  final int? initialIndex;
  final List<Widget> tabs;
  final List<Widget> pages;
  final TabStyle? selectLabelStyle;
  final TabStyle? labelStyle;

  @override
  State<TabBlock> createState() => _TabBlockState();
}

class _TabBlockState extends State<TabBlock> {
  var tag = Random().nextInt(10000);
  late final TabBlockController controller;
  @override
  void initState() {
    controller = Get.put(TabBlockController(initialIndex: widget.initialIndex),
        tag: "$tag");
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<TabBlockController>(
        tag: "$tag",
        builder: (controller) {
          return Column(
            children: [
              SizedBox(
                width: Get.width,
                child: LadderTabBar(
                  tabs: widget.tabs,
                  tag: "$tag",
                  selectLabelStyle: widget.selectLabelStyle,
                  labelStyle: widget.labelStyle,
                ),
              ),
              Container(
                  width: Get.width,
                  clipBehavior: Clip.hardEdge,
                  decoration: BoxDecoration(
                    color: widget.cardStyle?.color ?? Colours.white,
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(controller.index == 0
                            ? 0
                            : widget.cardStyle?.radius ?? 8.0),
                        topRight: Radius.circular(controller.index == 1
                            ? 0
                            : widget.cardStyle?.radius ?? 8.0),
                        bottomLeft:
                            Radius.circular(widget.cardStyle?.radius ?? 8.0),
                        bottomRight:
                            Radius.circular(widget.cardStyle?.radius ?? 8.0)),
                    boxShadow: [
                      widget.cardStyle?.shadow ??
                          const BoxShadow(
                              color: Color(0x0C0F499F),
                              offset: Offset(0, 10),
                              blurRadius: 12),
                    ],
                  ),
                  child: widget.pages[controller.index])
            ],
          );
        });
  }
}

class TabBlockController extends GetxController {
  TabBlockController({this.initialIndex});

  int? initialIndex;
  int _index = 0;

  int get index => _index;

  set index(int value) {
    _index = value;
    update();
  }

  @override
  void onInit() {
    index = initialIndex ?? 0;
    super.onInit();
  }
}

class CardStyle {
  const CardStyle({
    this.color,
    this.padding,
    this.shadow,
    this.radius,
    this.margin,
    this.gradient,
    this.height,
    this.backImg,
  });

  final double? radius;
  final Color? color;
  final String? backImg;
  final EdgeInsets? padding;
  final EdgeInsets? margin;
  final BoxShadow? shadow;
  final Gradient? gradient;
  final double? height;

  CardStyle copyWith({
    double? radius,
    Color? color,
    String? backImg,
    EdgeInsets? padding,
    EdgeInsets? margin,
    BoxShadow? shadow,
    Gradient? gradient,
    double? height,
  }) {
    return CardStyle(
        radius: radius ?? this.radius,
        color: color ?? this.color,
        backImg: backImg ?? this.backImg,
        padding: padding ?? this.padding,
        margin: margin ?? this.margin,
        shadow: shadow ?? this.shadow,
        gradient: gradient ?? this.gradient,
        height: height ?? this.height);
  }
}
